import {HOST, PASSWD, PORT, USER, CHARSET, DB} from "./dbinfo.js"
import mysql from "mysql"

function getConnecion() {
    let conn = mysql.createConnection({
        user: USER,
        password: PASSWD,
        host: HOST,
        port: PORT,
        database: DB,
        charset: CHARSET
    });
    return conn;
};





function getCountByArea() {
    let conn = getConnecion();
    const sqlByArea = "SELECT DATE(t_his_telemeterdata.record_time) AS Date,COUNT(*) AS total FROM t_his_telemeterdata GROUP BY DATE(t_his_telemeterdata.record_time);";
    conn.query(sqlByArea, function(err, data, fields){
        if (err){
            console.log(err);
            return;
        }

        console.log(data);
        data.forEach(element => {
            console.log(element.Date + "\t" + element.total);
        });
        
    })
}

//getCountByArea();

function updateData() {
        let conn = getConnecion();
        const SQL = "UPDATE t_his_telemeterdata SET t_his_telemeterdata.mete_value =  ? \
    WHERE t_his_telemeterdata.mete_value = ?";
        conn.query(SQL, [953149.43800, 953], function(err, result){
            if (err) {
                console.log(err);
                return 0;
            }
    
            console.log("updated successfully");
            console.log("updated " + result.affectedRows + " row(s)");
        })
    }
    
    // updateData();

    function addData1() {
            let conn = getConnecion();
            let record = {
                "lsc_id": 1,
                "mete_kind": 1,
                "device_id": 0
            };
            const SQL = "INSERT INTO t_his_telemeterdata set ?";
            conn.query(SQL, record, function(err, result){
                if (err) {
                    console.log(err);
                    return;
                }
        
                console.log("inserted " + result.affectedRows + " record(s)");
            })
        }
        
        //addData1();
        
